package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContextUtils;
import com.itheima.common.R;
import com.itheima.domain.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物表
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    /**
     * 查询购物车
     */
    @Autowired
    private ShoppingCartService shoppingCartService;
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //设置查询条件
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContextUtils.getId()).orderByDesc(ShoppingCart::getCreateTime);
        //查询购物表
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){
        //设置 userId
        shoppingCart.setUserId(BaseContextUtils.getId());
        log.info("shopping={}",shoppingCart);
        //获取 菜品 id
        Long dishId = shoppingCart.getDishId();
        //设置查询条件 1
        LambdaQueryWrapper<ShoppingCart> lqw =new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContextUtils.getId());
        //判断 购物单中为 菜品还是套餐
        if (dishId!=null){
            //设置查询条件 2
            lqw.eq(ShoppingCart::getDishId,dishId);
            //查询购物表
            ShoppingCart data = shoppingCartService.getOne(lqw);
            if (data!=null){
                //设置 菜品数量
                Integer number = data.getNumber();
                shoppingCart.setNumber(number+1);
                //修改菜品数量
                shoppingCartService.update(shoppingCart,null);
            }else{
                //表中不存在该数据 ，添加该数据
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartService.save(shoppingCart);
            }
        }else {
            //判断 购物单中为 菜品还是套餐
            //设置查询条件 2
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            //查询购物表
            ShoppingCart data = shoppingCartService.getOne(lqw);
            if (data!=null){
                Integer number = data.getNumber();
                shoppingCart.setNumber(number+1);
                //修改菜品数量
                shoppingCartService.update(shoppingCart,null);

            }else{
                //表中不存在该数据 ，添加该数据
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartService.save(shoppingCart);
            }
        }
        return R.success("添加成功");
    }

    /**
     * 清空购物单
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        //获取 userId
        Long userId = BaseContextUtils.getId();
        //设置条件
        LambdaQueryWrapper<ShoppingCart> lqw= new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        //删除数据
        shoppingCartService.remove(lqw);
        return R.success("操作成功");
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        //设置用户Id
        Long userId = BaseContextUtils.getId();
        shoppingCart.setUserId(userId);
        // 获取 菜品 id
        Long dishId = shoppingCart.getDishId();
        //设置条件
        LambdaQueryWrapper<ShoppingCart> lqw =new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        //判断 菜品id 是否为空
        if (dishId!=null){
            //设置条件 2
            lqw.eq(ShoppingCart::getDishId,dishId);
            //查询 购物表
            ShoppingCart data = shoppingCartService.getOne(lqw);
            Integer number = data.getNumber()-1;
            // 判断 菜品数量-1
            //菜品数量-1 >0 执行修改操作
            if (number>0){
                shoppingCart.setNumber(number);
                shoppingCartService.update(shoppingCart,null);
                //菜品数量-1 =0 执行删除操作
            }else if (number==0){
                //设置条件
                LambdaQueryWrapper<ShoppingCart> lqw2 =new LambdaQueryWrapper<>();
                lqw2.eq(ShoppingCart::getDishId,dishId);
                // 删除 数据
                shoppingCartService.remove(lqw2);
            }
        }else {
            //设置条件 2
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            ShoppingCart data = shoppingCartService.getOne(lqw);
            Integer number = data.getNumber()-1;
            if (number>0){
                shoppingCart.setNumber(number);
                shoppingCartService.update(shoppingCart,null);
            }else if (number==0){
                LambdaQueryWrapper<ShoppingCart> lqw2 =new LambdaQueryWrapper<>();
                lqw2.eq(ShoppingCart::getDishId,dishId);
                shoppingCartService.remove(lqw2);
            }
        }
        return R.success("操作成功");
    }


}
